//
//  ImageDetailVC.m
//  JadeShop
//
//  Created by mac on 2019/3/18.
//  Copyright © 2019年 SZJ.test. All rights reserved.
//

#import "ImageDetailVC.h"

#import "ImagePickerSelectView.h"
#import "ImageDetailCell.h"
#import "Tool.h"
@interface ImageDetailVC ()<BaseCollectionVCInterface,UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet ImagePickerSelectView *selectView;

@property (strong, nonatomic)UIPanGestureRecognizer *pan;

@property (nonatomic,strong) UISwipeGestureRecognizer * swipe;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nav_h;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tab_h;

@property (weak, nonatomic) IBOutlet UIView *nav_v;
@property (weak, nonatomic) IBOutlet UIView *tab_v;



@end

@implementation ImageDetailVC
{
    
    __weak IBOutlet UICollectionViewFlowLayout *_flowLayout;
    __weak IBOutlet UICollectionView *_collectionView;
    
    BOOL _finish;
    CGFloat _lastX;
    
    NSInteger _type; //1 平移  2轻扫  0 重置
}

@synthesize collectionView = _collectionView;
@synthesize flowLayout = _flowLayout;
- (void)viewDidLoad {
    self.collectionInterface = self;
    [super viewDidLoad];
    self.hidNav = YES;
    
    [_selectView setText:@"0"];
    _finish = YES;
    _type = 0;
    _nav_h.constant = CC_FRAME_NAV_X(64);
    _tab_h.constant = CC_FRAME_X(49);
    self.pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    self.pan.delegate = self;
    [self.view addGestureRecognizer:self.pan];
   
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [_collectionView scrollToItemAtIndexPath:_indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}



-(NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"ImageDetailCell"]];
}

-(void)collectionViewSelectAtIndexPath:(NSIndexPath *)indexPath{
    
    _nav_v.hidden = !_nav_v.hidden;
    _tab_v.hidden = !_tab_v.hidden;
    
}

-(NSInteger)sectionCount{
    return 1;
}

-(NSInteger)itemsCount:(NSInteger)section{
    return self.dataSouece.count;
}

-(BaseModel *)modelForCell:(NSIndexPath *)indexPath{
    return self.dataSouece[indexPath.item];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, -20, 0, 0);//第一个cell居中的效果,调用一次   上 左 下 右 的偏移量
    
}

- (void)panAction:(UIPanGestureRecognizer *)pan{
    if (pan.state == UIGestureRecognizerStateBegan) {
        _finish = NO;
    }
   
    
    CGPoint point = [pan translationInView:self.view];
    CGPoint ver = [pan velocityInView:self.view];
    
    CGPoint collectionPoint = self.collectionView.contentOffset;
    
    int index = collectionPoint.x / (YYScreenWidth() + 20);
    
    CGFloat width = YYScreenWidth() + 20;

    if (ver.x < -200 && !_finish && _type == 0) {
        index++;
        NSLog(@"!!!!!!!!!!!轻扫");
        _type = 2;
         [self move:index];
        
        return;
    }
    if (ver.x > 200 && !_finish && _type == 0) {
        index--;
         NSLog(@"!!!!!!!!!!!轻扫");
        if (index < 0) return;
        _type = 2;
        [self move:index];
       
        return;
    }
    if (!_finish && (_type == 0 || _type == 1)) {
        NSLog(@"平移");
        collectionPoint.x -= point.x - _lastX;
        
        _lastX = point.x;
        _type = 1;
        self.collectionView.contentOffset = collectionPoint;
        
        if (pan.state == UIGestureRecognizerStateEnded) {
            _lastX = 0;
            CGFloat longA = fmod(self.collectionView.contentOffset.x,width)  ;
            NSInteger indexa = self.collectionView.contentOffset.x / width;
            if (longA > width / 2) {
                indexa ++;
            }
            [self move:indexa];
        }
    }
    
    
 
    
}

- (void)move:(int)index{
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
    _finish = YES;
    _type = 0;
    NSLog(@"分割线——————————————————————————————————————");
}



-(BOOL)prefersStatusBarHidden
{
    return YES;// 返回YES表示隐藏，返回NO表示显示
}


@end
